/*
 *  WaveFunctionXSinX.cpp
 *  SoundController0002
 *
 *  Created by jiro on 10/11/20.
 *  Copyright 2010 jiro music. All rights reserved.
 *
 */

#include "WaveFunctionXSinX.h"

namespace sc {

	double WaveFunctionXSinX::operator() ( double x ) const 
	{
		x = ModifyX( x );
		return normalizer * std::pow( slopeOfX * x + interceptOfX, powerOfX ) * 
			std::sin( 2 * Pi * cycle * ( slopeOfX * x + interceptOfX ) );
	}

	bool WaveFunctionXSinX::operator == ( const WaveFunction& rhs ) const
	{
		if ( this->WaveFunction::operator == ( rhs ) )
		{
			const WaveFunctionXSinX& ref = dynamic_cast< const WaveFunctionXSinX& >( rhs );
			if ( powerOfX == ref.powerOfX &&
				 slopeOfX == ref.slopeOfX &&
				 interceptOfX == ref.interceptOfX ) {
				return true;
			}
		}
		return false;
	}

	bool WaveFunctionXSinX::operator != ( const WaveFunction& rhs ) const
	{
		return not( *this == rhs );
	}
	
	bool WaveFunctionXSinX::operator < ( const WaveFunction& rhs ) const
	{
		if ( type != rhs.GetType() ) {
			return type < rhs.GetType();
		}
		if ( this->WaveFunction::operator < ( rhs ) ) {
			return true;
		}
		const WaveFunctionXSinX& ref = dynamic_cast< const WaveFunctionXSinX& >( rhs );
		if ( powerOfX != ref.powerOfX ) {
			return powerOfX < ref.powerOfX;
		}
		if ( slopeOfX != ref.slopeOfX ) {
			return slopeOfX < ref.slopeOfX;
		}
		if ( interceptOfX != ref.interceptOfX ) {
			return interceptOfX < ref.interceptOfX;
		}
		return false;
	}

	double WaveFunctionXSinX::GetPowerOfX() const 
	{
		return powerOfX;
	}

	WaveFunctionXSinX::WaveFunctionXSinX( double powerOfX, double cycle, bool isReverse, bool isNormalize ) 
		: WaveFunction( Type_XSineX, 1 / cycle, Range( 0, cycle / 2.0 ), 1, isReverse ? -1 : 1, isReverse ? cycle / 2.0 : 0 ),
		  powerOfX( powerOfX )
	{
		if ( isNormalize ) {
			SetNormalizer();
		}
	}

	WaveFunctionXSinX::WaveFunctionXSinX( double powerOfX, double cycle, Range range, bool isReverse, bool isNormalize )
		: WaveFunction( Type_XSineX, 1 / cycle, range, 1, isReverse ? -1 : 1, isReverse ? range.end : 0 ),
		  powerOfX( powerOfX )
	{
		if ( isNormalize ) {
			SetNormalizer();
		}
	}
}